package com.cat.dynamicPlanning;

import java.util.Arrays;

/**
 * @author 曲大人的喵
 * @description https://leetcode.cn/problems/predict-the-winner/
 * @create 2025/9/5 20:46
 * @since JDK17
 */

public class Solution80 {
    public boolean predictTheWinner(int[] nums) {
        int n = nums.length, sum = Arrays.stream(nums).sum();
        int[][] dp = new int[n][n];
        for (int i = 0; i < n - 1; i++) {
            dp[i][i] = nums[i];
            dp[i][i + 1] = Math.max(nums[i], nums[i + 1]);
        }
        dp[n - 1][n - 1] = nums.length;
        for (int i = n - 3; i >= 0; i--) {
            for (int j = i + 1; j < n; j++) {
                int p1 = nums[i] + Math.min(dp[i + 2][j], dp[i + 1][j - 1]);
                int p2 = nums[j] + Math.min(dp[i + 2][j], dp[i + 1][j - 1]);
                dp[i][j] = Math.max(p1, p2);
            }
        }

        return dp[0][n - 1] * 2 >= sum;
    }

}
